from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
from rest_framework.response import Response
from django.http import HttpResponse
from meiduo_mall.libs.captcha.captcha import captcha
from django_redis import get_redis_connection
from . import constants
import random
import logging
from meiduo_mall.libs.yuntongxun.sms import CCP
from . import  serializers
from celery_tasks.sms.tasks import send_sms_code

# Create your views here.
# 日志记录器
logger = logging.getLogger('django')

# url('^image_codes/(?P<image_code_id>[\w-]+)/$', views.ImageCodeView.as_view()),
class ImageCodeView(APIView):
    """提供图片验证码"""

    def get(self,request,image_code_id):
        # 生成图片验证码:text存储到redis数据库，image相应到用户
        text,image = captcha.generate_captcha()
        # 存储图片验证码的内容
        redis_conn = get_redis_connection('verify_codes')

        redis_conn.setex('img%s'% image_code_id,constants.IMAGE_CODE_REDIS_EXPIRES,text)
        # 响应图片验证码图片数据
        return HttpResponse(image,content_type='image/jpg')

# url('^sms_codes/(?P<mobile>1[3-9]\d{9})/$', views.SMSCodeView.as_view()),
class SMSCodeView(GenericAPIView):#GenericAPIView
    """短信验证码
    需要传入的参数：mobile：手机号
                image_code_id：图片验证码编号
                text：用户输入的图片验证码
                """
    # 制定序列化器 用作校验
    serializer_class = serializers.ImageCodeCheckSerializer

    def get(self,request,mobile):
        # 接受参数
        # 校验参数

        # 获取序列化器对象 需要将text和image_code_id这两个查询字符串传入到序列化器对象中进行校验
        serializer = self.get_serializer(data=request.query_params)
        # 开启校验
        serializer.is_valid(raise_exception=True)


        # 生成短信验证码
        sms_code = '%06d' % random.randint(0,999999)
        print('验证码------------------',sms_code)
        logger.debug(sms_code)

        # 储存短信验证码到redis
        redis_conn = get_redis_connection('verify_codes')
        # redis_conn.setex('sms_%s' % mobile,constants.SMS_CODE_REDIS_EXPIRES,sms_code)
        # print('11111111111111111111111111')
        # 记录发送短信的标记
        # redis_conn.setex('send_flag_%s'% mobile,constants.SMS_CODE_REDIS_INTERVAL,1)
        # redis管道，可以将多个redis指令集成到一起执行，不用多次访问redis数据库
        pl = redis_conn.pipeline()
        pl.setex('sms_%s' % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code)

        # 记录发送短信的标记
        pl.setex('send_flag_%s' % mobile, constants.SMS_CODE_REDIS_INTERVAL, 1)
        # 开启执行
        pl.execute()

        # 发送短信验证码

        # CCP().send_template_sms(mobile,[sms_code,constants.SMS_CODE_REDIS_EXPIRES // 60],constants.SMS_SEND_TEMPLATE_ID)

        # 执行异步任务:delay将延迟异步任务发送到redis
        send_sms_code.delay(mobile,sms_code)

        # print("ok")
        # 响应json给vue
        return Response({'message':'ok'})
